#include "Cube.h"
using namespace Ogre;

Cube::Cube(Vector3 location, std::string type, GridNode* g, Ogre::MeshPtr cubePtr, int iD)
{
	this->iD = iD;
	myGNode = g;
	this->type = type;

	std::stringstream sID;
	sID << iD;

	mCubeEntity = sceneMgr->createEntity(type + sID.str(), "MasterCube.mesh", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
	
	mCubeNode = sceneMgr->getRootSceneNode()->createChildSceneNode(type + sID.str() + "Node", location);
	mCubeNode->attachObject(mCubeEntity);


	//faces were not in order as subentities the way we wanted, so I made sure they would be!
	//BDEFAC
	faceD = mCubeEntity->getSubEntity(0);
	faceB = mCubeEntity->getSubEntity(1);
	faceF = mCubeEntity->getSubEntity(2);
	faceE = mCubeEntity->getSubEntity(3);
	faceA = mCubeEntity->getSubEntity(4);
	faceC = mCubeEntity->getSubEntity(5);

//	mCubeNode->setScale(.1015625f, .1015625f, .1015625f);
}

std::string Cube::getName()
{
	return mCubeNode->getName();
}

GridNode* Cube::getGridNode()
{
	return myGNode;
}
std::string Cube::getType()
{
	return type;
}

Ogre::SceneNode* Cube::getSceneNode()
{
	return mCubeNode;
}

/*
get the current face
then create vector of vectors on face
int main()
{
  v = vector<vector<int> > items ( 3, vector<nodes> ( 3 ) );
  int k = 0;
 
  for ( int i = 0; i < 3; i++ ) {
    for ( int j = 0; j < 3; j++ )
      items[i][j] = k++;
  }

  if we know exit location(s) on prev cube, store it(them)
-find prev cube location on grid
-have prev node tell which directions are available, highlight available node and only allow user to move that node?
-find current cube location on grid
tell current node where it is with respect to previous node
nodeLocationFromPrev = left;

  then find which part of the vector is touching
 
 int touchAt = touchesPreviousCube()

 if 0,
check 0, 1, 2 for connections (true or false)
if 2, 

  int touchesPreviousCube()
  {

  //attach object to cube face, if that y=0, x > 0, facing right
	
  if y=0, x < 0 facing left
  if y>0, x=0 facing upright
  if y<0, x=0 facing down

  if not upright, reorient cube data
  if vector pointing down, old face vector gets replaced with new face vector
  0,0 -> 2,2
  0,1->2,1
0,2->2,0
current cube object reset to upright by moving it to y>0 x=0

THERE WILL BE NO CUBE ROTATION... JUST INITIAL ORIENTATIONS

if prevCube exited at 0,0 to left(it could exit up or left from 0,0)
then, current cube data to check is 0,2  
if there's no connections there, dont allow connection of cubes!



	

  or ???

  check the x and y positions on corners of the face
  corner 1 = v.at(0,0);
  corner 2 = v.at(0,2);
  corner 3 = v.at(3,0);
  corner 4 = v.at(3,2);
  int topCornerX = compareTopX;//gets the top left corner
  int topCornerY = compareTopY;
 
  fill in matrix ?? how??
  if top corner is corner 1
  //do nothing
  if top corner is corner 2
  

  function compareworldX
	
	if corner1.worldX < corner2.worldX


  function compareworldY

  */




void Cube::printSubEntitiesToFile(Ogre::Entity* _BodyEntity,char *outFile)
{
	//Ogre::Entity* _BodyEntity = sceneMgr->createEntity(modelName, modelName);
	if(_BodyEntity != NULL){
		std::ofstream _fileHandle;
		_fileHandle.open (outFile);
		if(_fileHandle == NULL)
		{
			std::cerr << "unable to create/open: " << _BodyEntity->getName();
		}
		else
		{
			int numSubs = _BodyEntity->getNumSubEntities();
			for(int i = 0; i < numSubs; i++)
			{
				_fileHandle << _BodyEntity->getSubEntity(i)->getMaterialName() << "\n";
			}
			/*Ogre::Skeleton::BoneIterator boneIter= _BodyEntity->getSkeleton()->getBoneIterator();
			while(boneIter.current() != boneIter.end())
			{

				_fileHandle << boneIter.peekNext()->getName() << "\n";
				boneIter.moveNext();
			}*/
		}
		//sceneMgr->destroyEntity(_BodyEntity);
	}
}